package com.jingyanzi.member.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.member.api.DicClientAPI;
import com.jingyanzi.member.dao.MCustomerExtraMapper;
import com.jingyanzi.member.domain.CustomerExtraDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;
import com.jingyanzi.member.entity.MCustomerExtra;
import com.jingyanzi.member.entity.MCustomerExtraExample;
import com.jingyanzi.member.query.CustomerQueryDO;
import com.jingyanzi.member.service.ICustomerExtraService;
import com.jingyanzi.member.util.BeanUtilsExtends;
import com.jingyanzi.sys.domain.DictionariesDO;

@Service("customerExtraService")
public class CustomerExtraServiceImpl extends BaseServiceImpl implements ICustomerExtraService {

	@Autowired
	private MCustomerExtraMapper customerExtraMapper;

	@Autowired
	private DicClientAPI dicClientAPI;

	@Override
	public ResultDO add(CustomerExtraDO domain) {

		MCustomerExtra entity = new MCustomerExtra();
		ResultDO result = BeanUtilsExtends.copy(entity, domain);

		if (!result.isSuccess()) {
			result.setSuccess(false);
			return result;
		}

		int r = -1;
		entity.setCarDicId(0);
		entity.setExpensesDicId(0);
		entity.setHouseDicId(0);
		entity.setIncomeDicId(0);
		entity.setConsumeCount(0);
		entity.setConsumeDate(null);
		entity.setIsDelete(1);

		try {
			r = customerExtraMapper.insertSelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			r = -1;
		}

		if (r < 0) {
			result.setSuccess(false);
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, entity);
		return result;
	}

	@Override
	public ResultDO edit(CustomerExtraDO domain) {

		MCustomerExtra entity = new MCustomerExtra();
		ResultDO result = BeanUtilsExtends.copy(entity, domain);

		if (!result.isSuccess()) {
			result.setSuccess(false);
			return result;
		}

		int r = -1;

		try {
			r = customerExtraMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			r = -1;
		}

		if (r < 0) {
			result.setSuccess(false);
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, entity);
		return result;
	}

	@Override
	public ResultDO remove(Integer id) {

		ResultDO result = new ResultSupport();

		MCustomerExtra entity = new MCustomerExtra();

		int r = -1;

		entity.setId(id);
		entity.setIsDelete(-1);

		try {
			r = customerExtraMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			r = -1;
		}

		if (r < 0) {
			result.setSuccess(false);
		}

		return result;
	}

	@Override
	public ResultDO get(Integer id) {

		ResultDO result = new ResultSupport();

		MCustomerExtra entity = null;

		try {
			entity = customerExtraMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (entity == null) {
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, getCustomerExtraDOForDicValue(entity));
		return result;
	}

	public List<CustomerExtraDO> queryExtra(CustomerQueryDO query) {

		List<MCustomerExtra> list = null;

		MCustomerExtraExample example = new MCustomerExtraExample();
		MCustomerExtraExample.Criteria criteria = example.createCriteria();
		if (query.getConsumeCount() != null && query.getConsumeCount() != 0) {
			criteria.andConsumeCountEqualTo(query.getConsumeCount());
		}
		if (query.getMinMoney() != null && query.getMaxMoney() != null) {
			criteria.andConsumeMoneyBetween(query.getMinMoney(), query.getMaxMoney());
		}
		if (query.getStartDate() != null && query.getEndDate() != null) {
			Date startTime = new Date(Long.parseLong(query.getStartDate()));
			Date endTime = new Date((Long.parseLong(query.getEndDate()) + (24 * 60 * 60 * 1000)));
			criteria.andConsumeDateBetween(startTime, endTime);
		}

		try {
			list = customerExtraMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return getCustomerExtraDOList(list);
	}

	public CustomerExtraDO getCustomerExtraDOForDicValue(MCustomerExtra entity) {
		CustomerExtraDO domain = getCustomerExtraDO(entity);
		DictionariesDO dic = new DictionariesDO();
		if (!isNumberInValid(entity.getCarDicId())) {
			dic = dicClientAPI.get(entity.getCarDicId());
			if (dic != null) {
				domain.setCarName(dic.getDataItem());
			}
		}
		if (!isNumberInValid(entity.getHouseDicId())) {
			dic = dicClientAPI.get(entity.getHouseDicId());
			if (dic != null) {
				domain.setHouseName(dic.getDataItem());
			}
		}
		if (!isNumberInValid(entity.getIncomeDicId())) {
			dic = dicClientAPI.get(entity.getIncomeDicId());
			if (dic != null) {
				domain.setIncomeName(dic.getDataItem());
			}
		}
		if (!isNumberInValid(entity.getExpensesDicId())) {
			dic = dicClientAPI.get(entity.getExpensesDicId());
			if (dic != null) {
				domain.setExpensesName(dic.getDataItem());
			}
		}
		return domain;
	}

	/**
	 * 对象拷贝单个实体到对象中
	 * 
	 * @param obj
	 * @return 返回DO对象
	 */
	public CustomerExtraDO getCustomerExtraDO(MCustomerExtra entity) {
		CustomerExtraDO dst = new CustomerExtraDO();
		if (entity == null)
			return dst;
		return BeanUtilsExtends.copyProperties(dst, entity) ? dst : null;
	}

	/**
	 * 对象拷贝多个实体到列表中
	 * 
	 * @param List
	 * @return List
	 */
	public List<CustomerExtraDO> getCustomerExtraDOList(List<MCustomerExtra> list) {
		List<CustomerExtraDO> result = new ArrayList<CustomerExtraDO>();
		if (list != null && !list.isEmpty()) {
			for (MCustomerExtra entity : list) {
				CustomerExtraDO domain = getCustomerExtraDOForDicValue(entity);
				if (domain != null) {
					result.add(domain);
				}
			}
		}
		return result;
	}

}
